package com.keur.money.utils.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.databinding.DialogSignBinding
import com.keur.money.http.HttpHelper
import com.keur.money.utils.event.KeurEventType

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/8 15:50
 * @Annotation：签到成功提示dialog
 */
class SignDialog: ProDialog {

    private lateinit var binding: DialogSignBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    override fun initWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogSignBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.btnRedeem) //兑换优惠券
        setOnClickListener(R.id.imgClose)//关闭
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if (v?.id == R.id.btnRedeem){
            mCallback?.onClick(v)
        }
    }

    /**
     * 设置获得的积分
     */
    fun showMullaPoints(ponits: String){
        val ponitText = String.format(MyApp.app.getString(R.string.sign_point), ponits)
        val showText = String.format(MyApp.app.getString(R.string.sign_point_today),ponitText)
        val startIndex = showText.indexOf(ponitText)

        val spString = SpannableString(showText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FF8027FF")), startIndex, startIndex + ponitText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spString.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + ponitText.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        binding.textPoints.text = spString
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
        //dismiss
        fun onDismiss()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_SIGN_SUCCESS_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        mCallback?.onDismiss()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_SIGN_SUCCESS_DISMISS,eventRemark)
    }
}